package lz.controls
{
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.external.ExternalInterface;
	import flash.text.TextField;

	/**
	 * 提示框
	 * @author lxh
	 * 
	 */	
	public class Alert extends Sprite
	{
		private static var alert :oAlert;
		
		public static const YES :int = 1;
		public static const NO  :int = 2;
		
		public function Alert()
		{
			super();
		}
		/**
		 *显示提示框 
		 * @param text
		 * @param title
		 * @param flags
		 * @param parent
		 * @param closeHandler
		 * @param iconClass
		 * @param defaultButtonFlag
		 * @return 
		 */		
		private static var closeFunc :Function;
		private static var markBg :Sprite;
		public static function show(text:String = "",
									title:String = "", 
									flags:uint = 0x4, 
									parent :DisplayObjectContainer = null, 
									closeHandler:Function = null,
									iconClass:Class = null, 
									defaultButtonFlag:uint = 0x4):DisplayObject
		{
			if(parent == null)
			{
				return null;
			}
			if(alert == null)
			{
				alert  = new oAlert();
			}
			if(markBg == null)
			{
				markBg = new Sprite();
			}
			else
			{
				markBg.graphics.clear();
			}
			closeFunc = closeHandler;
			yesLabel = "确定";
			noLabel = "取消";
			alert.show(text,flags,onAlertClose);
			////
			
			if(parent.stage)
			{
				alert.x =  (GlobalDef.GameStage.stageWidth - alert.width)>>1;
				alert.y = (GlobalDef.GameStage.stageHeight - alert.height)>>1;
				markBg.graphics.beginFill(0x999999,0);
				markBg.graphics.drawRect(0,0,GlobalDef.GameStage.stageWidth,GlobalDef.GameStage.stageHeight);
				markBg.graphics.endFill();
				if(markBg.parent == null)
				{
					parent.addChild(markBg);
				}
			}
			/////
			if(alert.parent == null)
			{
				parent.addChild(alert as DisplayObject);
				alert.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
			}
			(alert as DisplayObject).visible = true;
			return (alert as DisplayObject);
		}
		/**
		 *显示通知信息 可以多个oAlert 
		 * @param text
		 * @param title
		 * @param flags
		 * @param parent
		 * @param closeHandler
		 * @param iconClass
		 * @param defaultButtonFlag
		 * * @param timeOut  倒计时关闭
		 * @return 
		 * 
		 */		
		public static function showNotice(text:String = "",
									title:String = "", 
									flags:uint = 0x4, 
									parent :DisplayObjectContainer = null, 
									closeHandler:Function = null,
									iconClass:Class = null, 
									defaultButtonFlag:uint = 0x4,
									timeOut:int = 0):DisplayObject
		{
			if(parent == null)
			{
				return null;
			}
			var nAlert :oAlert = new oAlert();
			nAlert.show(text,flags,closeHandler);
			nAlert.timeOut = timeOut;
			////
			nAlert.x =  (GlobalDef.GameStage.stageWidth - nAlert.width)>>1;
			nAlert.y = (GlobalDef.GameStage.stageHeight - nAlert.height)>>1;
			/////
			parent.addChild(nAlert as DisplayObject);
			(nAlert as DisplayObject).visible = true;
			return (nAlert as DisplayObject);
		}
		/**
		 *显示错误提示信息 
		 * @param text
		 * @param parent
		 * @param closeHandler
		 * Alert.showErrorTip("游戏币不足!\n金币不足!",GlobalDef.GameStage);
		 */		
		public static function showErrorTip(text:String = "",
											parent :DisplayObjectContainer = null, 
											closeHandler:Function = null):void
		{
			if(text==null || text=="")
			{
				return;
			}
			var oErrorTip :MovieClip = Reflection.createInstance("com.liangzhong.ui.oErrorTip");
			if(oErrorTip == null)return;
			if(parent == null)parent = GlobalDef.GameStage;
			parent.addChild(oErrorTip);
			oErrorTip.mouseChildren = false;
			oErrorTip.mouseEnabled = false;
			var txtRemind :TextField = oErrorTip.item.txtRemind;
			txtRemind.htmlText = text + "";
			if(txtRemind.textWidth < 500)
			{
				txtRemind.width = txtRemind.textWidth + 5;
			}
			else
			{
				txtRemind.width = 500;
			}
			txtRemind.height = txtRemind.numLines * 24;
			oErrorTip.x = (GlobalDef.GameStage.stageWidth - oErrorTip.width)>>1;
			oErrorTip.y = (GlobalDef.GameStage.stageHeight - oErrorTip.height)>>1;
			oErrorTip.addFrameScript(oErrorTip.totalFrames - 1,onCompleteF);
			function onCompleteF():void
			{
				if(closeHandler!=null)closeHandler.call();
				if(oErrorTip && oErrorTip.parent)
				{
					oErrorTip.parent.removeChild(oErrorTip);
					oErrorTip = null;
				}
			}
		}
		/**
		 *关闭事件 
		 * @param type
		 * 
		 */		
		private static function onAlertClose(type :int):void
		{
			if(markBg && markBg.parent)
			{
				markBg.parent.removeChild(markBg);
				markBg = null;
			}
			if(closeFunc != null)
			{
				closeFunc(type);
				closeFunc = null;
			}
		}
		/**
		 * 
		 * @param e
		 * 
		 */		
		private static function onRemoveFromStage(e :Event):void
		{
			if(markBg && markBg.parent)
			{
				markBg.parent.removeChild(markBg);
				markBg = null;
			}
			if(alert)
			{
				alert.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
				alert = null;
			}
		}
		/**
		 *清理 
		 * 
		 */		
		public static function clear():void
		{
			
		}
		/**
		 * 设置确认标签文本
		 * @param value
		 * 
		 */		
		public static function set yesLabel(value :String):void
		{
			if(alert)
			{
				alert.yesLabel = value;
			}
		}
		/**
		 * 
		 * @param value
		 * 
		 */		
		public static function set noLabel(value :String):void
		{
			//oAlert.noLabel = value;
		}
		/**
		 *钱币不够 提示
		 */		
		public static function noCashAlert(value:String = "点券不足，是否前往充值？"):void
		{
			Alert.show(value,"",Alert.YES | Alert.NO,GlobalDef.AlertContainer,noCashAlertH);
			Alert.yesLabel = "充值";
		}
		/**
		 *分解确认 
		 * @param type
		 * 
		 */		
		private static function noCashAlertH(type :int):void
		{
			if(type == Alert.YES)
			{
				if(ExternalInterface.available)
				{
					ExternalInterface.call("app_recharge",1);
				}
			}
		}
	}
}